from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SKUImage, SKU
from meiduo_admin.serializers.skus import SKUImageSerializer, SKUSimpleSerializer


# GET  /meiduo_admin/skus/images/?page=<页码>&pagesize=<页容量>
class SKUImageViewSet(ModelViewSet):
    permission_classes = [IsAdminUser]

    # 指定router动态生成路由时，提取参数的正则表达式
    lookup_value_regex = '\d+'

    # 指定视图所使用的查询集
    queryset = SKUImage.objects.all()

    # 指定视图所使用的序列化器类
    serializer_class = SKUImageSerializer

    # def list(self, request, *args, **kwargs):
    #     queryset = self.get_queryset()
    #     serializer = self.get_serializer(queryset,many=True)
    #     return Response(serializer.data)

    # def create(self, request, *args, **kwargs):
    #     serializer = self.get_serializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save()
    #
    #     return Response(serializer.data,status=201)
    # def retrieve(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     serializer = self.get_serializer(instance)
    #     return Response(serializer.data)

    # def update(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     serializer =self.get_serializer(instance,data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save()
    #     return Response(serializer.data)

    # def destroy(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     instance.delete()#此操作FDFS中的图片不会被删除
    #     return Response(status=204)

class SKUSimpleView(ListAPIView):
    permission_classes = [IsAdminUser]

    #指定视图所使用的查询集
    queryset = SKU.objects.all()

    #指定视图所使用的序列化器类
    serializer_class = SKUSimpleSerializer

    #注意关闭分页
    pagination_class = None